package com.jsecode.test.protocol.gid22;

import java.util.List;

import com.jsecode.lib.util.LoggerUtil;
import com.jsecode.protobuf.VehicleCan.Can;
import com.jsecode.protocol.gid22.GidCan;
import com.jsecode.protocol.gid22.GidHeader;
import com.jsecode.protocol.gid22.GidMsgID;
import com.jsecode.protocol.gid22.GidPack;
import com.jsecode.protocol.gid22.b2b.GidB2BUtil;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;

public class GidPackDemo extends GidPack {

    @Override
    public int checkHeader(GidHeader header){
        return super.checkHeader(header);
    }
    
    @Override
    public void sendResponse(ByteBuf sendBuf){
        LoggerUtil.getLogNetSend().info("send buf:\n{}", ByteBufUtil.hexDump(sendBuf));
        
        super.sendResponse(sendBuf);
    }
    
    @Override
    public void afterDecoded(GidPack pack){
        if (pack != null && pack.getErrCode() == 0) {
            int msgId = pack.getHeader().getMsgId();
            switch(msgId){
                case GidMsgID.CAN_FLAG:
                    logger.info("received can data.");
                    GidCan can = (GidCan)pack.getBody();
                    List<Can> list = GidB2BUtil.toProtoCan(pack.getHeader().getDeviceId(), can);
                    for (Can canItem : list) {
                        // logger.info("can = {}", canItem.toString());
                        LoggerUtil.getLogNetRecv().info("received, id: ", canItem.getDeviceId());
                    }
                    break;

                default:
                    break;
            }
        }
    }
    
}
